"""
utils related time
"""
import time
import signal

from .log import LOGGER


def print_run_time(func):
    """
    print time that used for running the func.
    """
    def wrapper(*args, **kwargs):
        cur_time = time.time()
        func(*args, **kwargs)
        LOGGER.info("%.2fs taken for running function [%s]",
                    time.time()-cur_time, func.__name__)
    return wrapper


def time_limit(set_time):
    """
    limit the time for excetuing the func.
    """
    def wraps(func):
        def handler(signum, frame):
            raise RuntimeError()

        def deco(*args, **kwargs):
            try:
                signal.signal(signal.SIGALRM, handler)
                signal.alarm(set_time)
                res = func(*args, **kwargs)
                signal.alarm(0)
                return res
            except RuntimeError:
                LOGGER.error("timeout")
        return deco
    return wraps
